package com.cdp.web.controller.system;
import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysFileAudit;
import com.cdp.system.dto.AuditRequestDTO;
import com.cdp.system.dto.SysFileAuditDTO;
import com.cdp.system.service.ISysFileAuditService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * 审核信息
 */
@RestController
@RequestMapping("/system/audit")
public class SysFileAuditController {


    @Autowired
    private ISysFileAuditService fileAuditService;

    /**
     * 中间表 测试用
     *
     * @return
     */
    @GetMapping("listFileAudits")
    private AjaxResult listFileAudits() {
        List<SysFileAudit> listFileAudits = fileAuditService.listFileAudits();

        ArrayList<SysFileAuditDTO> arrayList = new ArrayList<>();
        for (SysFileAudit listFileAudit : listFileAudits) {
            SysFileAuditDTO sysFileAuditDTO = new SysFileAuditDTO();
            BeanUtils.copyProperties(listFileAudit, sysFileAuditDTO);

            Integer tableName = sysFileAuditDTO.getTableName();
            sysFileAuditDTO.setTableNameStr(getTableNameStr(tableName));

            sysFileAuditDTO.setUserName(listFileAudit.getUserName());

            Integer status = sysFileAuditDTO.getStatus();
            sysFileAuditDTO.setStatusStr(getStatusStr(status));

            arrayList.add(sysFileAuditDTO);
        }

        return AjaxResult.success(arrayList);
    }

    /**
     * 根据材料类型代码获取对应的字符串描述
     * @param tableName 材料类型代码
     * @return 对应的字符串描述
     */
    private String getTableNameStr(Integer tableName) {
        if (tableName == null) {
            return "";
        }
        switch (tableName) {
            case 1: return "水泥";
            case 2: return "砂";
            case 3: return "石";
            case 4: return "粉煤灰";
            case 5: return "减水剂";
            case 6: return "增效剂";
            case 7: return "力学性能";
            case 8: return "变形性能";
            case 9: return "耐久性能";
            case 10: return "水";
            case 11: return "矿粉";
            case 12: return "其他参合料";
            case 13: return "膨胀剂";
            case 14: return "坍落度";
            default: return "未知类型";
        }
    }

    /**
     * 根据审核状态代码获取对应的字符串描述
     * @param status 审核状态代码
     * @return 对应的字符串描述
     */
    private String getStatusStr(Integer status) {
        if (status == null) {
            return "";
        }
        switch (status) {
            case 0: return "待审核";
            case 1: return "审核通过";
            case 2: return "审核未通过";
            default: return "未知状态";
        }
    }
/*    @GetMapping("listFileAudits")
    private AjaxResult listFileAudits() {
        List<SysFileAudit> listFileAudits = fileAuditService.listFileAudits();

        ArrayList<SysFileAuditDTO> arrayList = new ArrayList<>();
        for (SysFileAudit listFileAudit : listFileAudits) {
            SysFileAuditDTO sysFileAuditDTO = new SysFileAuditDTO();
            BeanUtils.copyProperties(listFileAudit, sysFileAuditDTO);

            Integer tableName = sysFileAuditDTO.getTableName();
            sysFileAuditDTO.setTableNameStr(getTableNameStr(tableName));
            Integer status = sysFileAuditDTO.getStatus();
            sysFileAuditDTO.setStatusStr();
            
            arrayList.add(sysFileAuditDTO);
        }

        return AjaxResult.success(arrayList);
    }*/



/*    @GetMapping("listFileAudits")
    private AjaxResult listFileAudits() {


        List<SysFileAudit> listFileAudits = fileAuditService.listFileAudits();

        ArrayList<SysFileAuditDTO> arrayList = new ArrayList<>();
        for (SysFileAudit listFileAudit : listFileAudits) {
            SysFileAuditDTO sysFileAuditDTO = new SysFileAuditDTO();
            BeanUtils.copyProperties(listFileAudit,sysFileAuditDTO);

            Integer tableName = sysFileAuditDTO.getTableName();
            sysFileAuditDTO.setTableNameStr();
            arrayList.add(sysFileAuditDTO);
        }

        return AjaxResult.success(arrayList);
    }*/



    /**
     * 管理员审核
     *
     * @return AjaxResult
     */
    @PostMapping("/audit")
    public AjaxResult updateAudit(@RequestBody AuditRequestDTO auditRequestDTO) {
        // 检查参数
        try {
            // 调用服务层更新审核
            return fileAuditService.updateAudit(auditRequestDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("审核失败: " + e.getMessage());
        }
    }





}


